package cn.tedu.blogproject.controller;

import cn.tedu.blogproject.common.response.JsonResult;
import cn.tedu.blogproject.common.response.StatusCode;
import cn.tedu.blogproject.mapper.UserMapper;
import cn.tedu.blogproject.pojo.dto.UserLoginDTO;
import cn.tedu.blogproject.pojo.dto.UserRegDTO;
import cn.tedu.blogproject.pojo.entity.User;
import cn.tedu.blogproject.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Api(tags = "用户管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/users/")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "注册方法")
    @PostMapping("reg")
    public JsonResult register(@RequestBody UserRegDTO userRegDTO){
        /**
         * 1.接受参数，校验参数的合法性
         * 2.检查该用户是否在数据库中存在SELECT接口查询
         * 2.1如果存在直接返回EXIST
         * 2.2如果不存在则调用INSERT接口来进行插入
         */
        log.debug("UserRegDTO: " + userRegDTO);
        String username = userRegDTO.getUsername();
        UserVO userVO = userMapper.selectByUserName(username);
        if(userVO != null){
            return new JsonResult(StatusCode.USERNAME_ALREADY_EXISTS);
        }
        User user = new User();
        user.setCreated(new Date());
        BeanUtils.copyProperties(userRegDTO,user);
        Integer res = userMapper.insertUser(user);
        log.debug("插入用户返回值：" + res);
        return JsonResult.ok();
    }

    @ApiOperation(value = "登录方法")
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session){
        /**
         * 1.接收参数，校验参数
         * 2.根据用户名先查询数据库
         * 2.1如果用户名不存在直接返回用户名错误
         * 2.2如果用户名正确
         * 3.再根据密码查询对应用户名的数据库中的密码
         * 3.1如果正确直接返回登录成功
         * 3.2不正确返回密码错误
         */
        log.debug("userLoginDTO: " + userLoginDTO);
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();

        UserVO userVO = userMapper.selectByUserName(username);
        if(userVO == null){
            return new JsonResult(StatusCode.USERNAME_ERROR);
        }
        String userVOPassword = userVO.getPassword();
        if(!userVOPassword.equals(password)){
            return new JsonResult(StatusCode.PASSWORD_ERROR);
        }
        session.setAttribute("user",userVO);
        return JsonResult.ok();
    }

    @ApiOperation("查看当前用户状态方法")
    @GetMapping("currentUser")
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        return JsonResult.ok();
    }

    @ApiOperation(value = "退出登录方法")
    @GetMapping("logout")
    public void logout(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
    }
}
